<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007-2008 Numinix Technology http://www.numinix.com    |
// |                                                                      |
// | Portions Copyright (c) 2003-2006 Zen Cart Development Team           |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
//  $Id: class.fec_checkbox.php 88 2009-08-27 21:03:25Z numinix $
//

class FECCheckboxObserver extends base 
{
	function FECCheckboxObserver()
	{
		global $zco_notifier;
		$zco_notifier->attach($this, array('NOTIFY_HEADER_START_CHECKOUT_SUCCESS'));
	}
	
	function update(&$class, $eventID, $paramsArray) 
	{
    if (FEC_CHECKBOX == 'true') {
      global $db;
      // find out the last order number generated for this customer account 
      $orders_query = "SELECT * FROM " . TABLE_ORDERS . "
                   WHERE customers_id = :customersID
                   ORDER BY date_purchased DESC LIMIT 1";
      $orders_query = $db->bindVars($orders_query, ':customersID', $_SESSION['customer_id'], 'integer');
      $orders = $db->Execute($orders_query);
      $orders_id = $orders->fields['orders_id'];
      
      // use order-id generated by the actual order process
      // this uses the SESSION orders_id, or if doesn't exist, grabs most recent order # for this cust (needed for paypal et al).
      // Needs reworking in v1.4 for checkout-rewrite
      $zv_orders_id = (isset($_SESSION['order_number_created']) && $_SESSION['order_number_created'] >= 1) ? $_SESSION['order_number_created'] : $orders_id;
      $orders_id = $zv_orders_id;
      
      // get the value of the checkbox
      $checkbox = ($_SESSION['fec_checkbox'] == '1' ? 1 : 0);
      
      // add checkbox value option to the orders table
      $db->Execute("UPDATE  " . TABLE_ORDERS . "
                    SET checkbox = '" . $checkbox . "'
                    WHERE orders_id = " . $orders_id . "
                    LIMIT 1");
      
      // unset sessions
      unset($_SESSION['fec_checkbox']);
    }
	}
}
// eof